import database from '../../../utils/database'
// 数据统计服务类
export class StatisticalService {
    getCount(type:number) {
        return new Promise((resolve, reject)=>{
            // 这几种直接查询数据的需求比较简单, 直接从数据库查询数量
            if (type == 0 || type == 2 || type == 4) {
                let tableName = ""
                let dateName = "created_at"
                if (type == 0) {
                    tableName = "users"
                    dateName = "registration_time"
                } else if (type == 2) {
                    tableName = "Orders"
                } else if (type == 4) {
                    tableName = "Evaluation"
                }
                database.dataCount(tableName, (data:any)=>{
                    if (data) {
                        let respose = {} as any
                        respose.allCount = data
                        database.dataWithinMonth(tableName, dateName, (datas:any)=>{
                            if (datas) {
                                respose.datas = datas
                                resolve(respose)
                            } else { reject('获取数据异常') }
                        })
                    } else { reject('获取数据异常') }
                })
            } else {
                // 用户日活数据整理
                if (type == 1) {
                    database.dataCountWithinLastday('users', 'last_login_time', (data:any)=>{
                        if (data) {
                            let respose = {} as any
                            respose.allCount = data
                            database.dataWithinMonth('users', 'last_login_time', (datas:any)=>{
                                if (datas) {
                                    respose.datas = datas
                                    resolve(respose)
                                } else { reject('获取数据异常') }
                            })
                        } else { reject('获取数据异常') }
                    })
                } 
                // 销售额数据
                if (type == 3) {
                    database.dataSum('orders', 'price', (data:any)=>{
                        if (data) {
                            let respose = {} as any
                            respose.allCount = data
                            database.dataSumWithinMonth('orders', 'created_at', 'price', (datas:any)=>{
                                if (datas) {
                                    respose.datas = datas
                                    resolve(respose)
                                } else { reject('获取数据异常') }
                            })
                        } else { reject('获取数据异常') }
                    })
                }
            }
        })
    }
}
// 导出服务实例
export default new StatisticalService();